package com.xinzhidi.web.netty.server;

import com.xinzhidi.web.netty.utils.FormatData;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/***
 * @author cx
 * @date 2023/4/3 15:17
 */
public class NettyClient {

    public static final String sheet1 = "07e700020000045d00000003e91d4c0001000103003c1d4c303030300000000000000000000000000000000000000000000000000000000000000000000000000001f404b004b01d4c0064003212c0";
    public static final String sheet2 = "090100030000006500000003ea138800c8021286000001720007005e000113820000007d0101550153020000000000000100000091";
    public static final String sheet5 = "07e700010000006500000003ed0000000000000000000000000000000001000000000000000000005d01f407d00032006400000000000000650000000000000000000000000000";

    public static final String sheet10 = "07e700030000045700000003f04367b8524367eb854367147b43c8d23e43c8e5e443c7ec6342e8ae1542d6ae15430d2e1446cc000046beb00046f87000479fd800c5a8000043a80000c5658000c607c00046cf000046bdc00046f8700047a128003132333435363730";

    public static final String sheet12 = "07e700020000045a00000003f8aa";

    public static final String sheet9 = "07e700010000006500000003ef010000000100000101010000000000000101000000010101000000000000";

    public static final String sheet14 = "090100030000006500000004b134307a6c313531390402";

    public static final String sheet14CWS = "07e700140000045a00000004b161626364313131341f03";

    public static final String sheet14CDZ = "090100030000006500000004b134307a6c313531390401";

    public static final String sheet15 = "07E703DB0000238F000000044D010000";

    public static final String ceShi = "FEFEFE684612233344445555040206207416";

    private String host;
    private int port;

    public NettyClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void run() throws InterruptedException {
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(workerGroup);
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new NettyClientHandler());
                }
            });

            // Start the client.
            ChannelFuture f = bootstrap.connect(host, port).sync();

            // Wait until the connection is closed.
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
//        String host = "43.143.229.55";
        String host = "192.168.101.103";
        int port = 9294;
        NettyClient client = new NettyClient(host, port);
        client.run();
    }

    public class NettyClientHandler extends ChannelInboundHandlerAdapter {

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            byte[] bytes = FormatData.hex2bytes(ceShi);
            System.out.println("Client connected to server: " + ctx.channel().remoteAddress());
            ByteBuf buf = ctx.alloc().buffer();
            buf.writeBytes(bytes);
            ctx.write(buf);
            ctx.flush();
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            System.out.println("=================>>>接收到返回的消息Client received data from server: " + msg);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
            // Close the connection when an exception is raised.
            ctx.close();
        }
    }
}
